package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ClassyTypeBean.ClassyGoods;

import java.util.ArrayList;

public class ClassyGridAdapter extends BaseAdapter {
    ArrayList<ClassyGoods> goods;
    Context context;
    LayoutInflater inflater;

    public ClassyGridAdapter(ArrayList<ClassyGoods> goods, Context context) {
        this.goods = goods;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return goods != null ? goods.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return goods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        if (convertView == null) {
            convertView = inflater
                    .inflate(R.layout.classy_type_grid_item, null);
        }
        holder.img = (ImageView) convertView
                .findViewById(R.id.classy_type_grid_img);
        holder.text = (TextView) convertView
                .findViewById(R.id.classy_type_grid_text);
        if (position < goods.size() && goods.get(position) != null) {
            MyApplication.loadImageByPicasso(context, goods.get(position).getImgurl(), null, holder.img, 0, 0, 1);
        }
        holder.text.setText(goods.get(position).getName());
        if (convertView.getTag() == null) {
            convertView.setTag(holder);
        }
        return convertView;
    }

    class Holder {
        ImageView img;
        TextView text;
    }
}
